//
//  hyChangeCityViewController.m
//  anjuke
//
//  Created by jim Yao on 16/1/1.
//  Copyright © 2016年 anjuke. All rights reserved.
//

#import "hyChangeCityViewController.h"
#import "AFNetworking.h"
#import "MJExtension.h"
#import "hyCityModel.h"
#import "hyCt.h"

@interface hyChangeCityViewController()
@property (nonatomic,strong)NSArray *cityArr;
@property (nonatomic, strong)NSArray *ctArr;
@end
@implementation hyChangeCityViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self setNavBar];
    [self loadData];
    
    
}

- (void)setNavBar{
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [closeBtn setImage:[UIImage imageNamed:@"comm_icon_close@2x.png.base.universal.regular.off.horizontal.normal.active.onepartscale.onepart.52303.000.00."] forState:UIControlStateNormal];
    [closeBtn setImage:[UIImage imageNamed:@"comm_icon_close_slt@2x.png.base.universal.regular.off.horizontal.normal.active.onepartscale.onepart.38540.000.00."] forState:UIControlStateHighlighted];
    [closeBtn addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:closeBtn];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 50)];
    titleLabel.text=@"切换城市";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    //    titleLabel.backgroundColor = [UIColor blueColor];
    self.navigationItem.titleView =titleLabel;

    
}

- (void)closeClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark ---代理


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    NSLog(@"%s",__func__);
    
    return _cityArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID =@"city";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    hyCityModel *model= _cityArr[indexPath.row];
//     _ctArr= [hyCt mj_objectArrayWithKeyValuesArray:model.ct];
    hyCt *ct = [hyCt mj_objectWithKeyValues:model.ct];
    
    cell.textLabel.text =ct.name;
    
//    NSMutableDictionary *dict= [[NSMutableDictionary alloc] initWithDictionary:model.ct];
//    cell.textLabel.text =dict[@"name"];

//    NSDictionary *dic = model.ct;
//    cell.textLabel.text = dic[@"name"];
    
//    NSLog(@"%@",model.ct);
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点了city");
}

- (void)loadData{
    
        AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    //    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    
    [manager GET:@"http://api.anjuke.com/mobile/1.3/city.mergedCity?api_key=d945dc04a511fcd7e6ee79d9bf4b9416&app=i-ajk&cid=-1&cv=9.2&from=mobile&i=943ACD38-38C2-487E-9580-201601020027&m=iPhone&macid=0f607264fc6318a92b9e13c65db7cd3c&o=iPhone%2520OS&ostype2=ios7&pm=A01&py=1&qtime=20160102002724&udid2=764624A3-1314-48DD-A661-201512122246&uuid2=943ACD38-38C2-487E-9580-201601020027&uuid=943ACD38-38C2-487E-9580-201601020027&v=7.0.4&sig=3ad55a2df3cd6e2c0b21cdcf39e1a10e" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        [responseObject writeToFile:@"/Users/jimyao1/Desktop/abc/a.plist" atomically:YES];
        //        responseObject[@"city"][@"name"];
        
        _cityArr = [hyCityModel mj_objectArrayWithKeyValuesArray:responseObject[@"city"]];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSArray *array = @[ @"A", @"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
    return array[section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSArray *array = @[@"热门城市",@"A", @"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];//背景色
    self.tableView.sectionIndexColor = [UIColor blueColor];//字体色
    return array;
}

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
//    NSLog(@"%@,%ld",title,(long)index);
//    NSLog(@"1234567890-");
    
    return 0;
}


@end






